/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.faces.context.FacesContext;
import queenb.net.entitybean.N350;
import queenb.web.inteface.N350Inf;
import queenb.web.store.N350Store;
import queenb.web.util.ContextUtil;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class N350Controller extends QueenbController {

    private int pn350;// Primakey
    private int fn100;// nhan vien
    private String nv351;// so CMND/Passport
    private Date nd352;// Ngay cap
    private Date nd353;// Ngay het han
    private String nv354;// notice
    private int nn355;//phan biệt CMND và Passport
    private int fh000;
    private int fh200;
    private String demo;
    private N350Inf n350Inf;
    private N350Store n350Store;
    private List<N350> listN350N;
    private String temp = "";
    private String temp_active = "display:none;";
    private String pagereload;
    private boolean flagedit;
    private String table1;
    private boolean add = false;
    private boolean edit = false;
    private boolean delete = false;
    private String nv354p;
    private String nv354c;
    private String nv351p;
    private String nv351c;
    private boolean nn355c = true;
    private boolean nn355p = false;
    private Date nd352c = null;
    private Date nd353c = null;
    private Date nd352p = null;
    private Date nd353p = null;
    private int pn350c = 0;
    private int pn350p = 0;
    private boolean renderAdd = true;
    private String messageform = "";
    private List<N350> listoftabN350 = new ArrayList<N350>();
    private String stringtab;//
    public N350Controller() {

        this.setTemp("display:block");
        n350Store = new N350Store();
//        try {
//            this.pagereload = getRequest().getParameter("page");
//            if (this.pagereload == null) {
//                this.pagereload = ContextUtil.getRequestParameter("page");
//            }
//            //this.pagereload = pages.toString();
//            System.out.println("**************************" + pagereload);
//        } catch (Exception ex) {
//        }
//        try {
//            if (pagereload.equals("employeenew.jsp")) {
//                if (getLoginSession().getRight().isEMP_CMND_CRE()) {
//                    add = true;
//                }
//                if (getLoginSession().getRight().isEMP_CMND_MOD()) {
//                    edit = true;
//                }
//                if (getLoginSession().getRight().isEMP_CMND_DEL()) {
//                    delete = true;
//                }
//            }
//             if (pagereload.equals("employee.jsp")) {
//                if (getLoginSession().getRight().isEMP_CMND_CRE()) {
//                    add = true;
//                }
//                if (getLoginSession().getRight().isEMP_CMND_MOD()) {
//                    edit = true;
//                }
//                if (getLoginSession().getRight().isEMP_CMND_DEL()) {
//                    delete = true;
//                }
//            }
//            if (pagereload.equals("supplier.jsp")) {
//                if (getLoginSession().getRight().isSUP_CMND_CRE()) {
//                    add = true;
//                }
//                if (getLoginSession().getRight().isSUP_CMND_MOD()) {
//                    edit = true;
//                }
//                if (getLoginSession().getRight().isSUP_CMND_DEL()) {
//                    delete = true;
//                }
//            }
//            if (pagereload.equals("customer.jsp")) {
//                if (getLoginSession().getRight().isCUS_CMND_CRE()) {
//                    add = true;
//                }
//                if (getLoginSession().getRight().isCUS_CMND_MOD()) {
//                    edit = true;
//                }
//                if (getLoginSession().getRight().isCUS_CMND_DEL()) {
//                    delete = true;
//                }
//            }
//        } catch (Exception ex) {
//        }
//        // 
        Object objPN100 = getRequest().getParameter("pn100");
        if (objPN100 != null) {
            this.fn100 = Integer.parseInt(objPN100.toString());
        }
        //Bien dat o day dung chung dua duoc.. luu y ai code phan nay thi nen de y nhung code sau
        Object fn100N = getRequest().getParameter("id");
        Object fn100A = getRequest().getParameter("fn100A");
        Object fn100M = getRequest().getParameter("fn100M");
        Object fn100E = getRequest().getParameter("fn100E");
        Object pn350E = getRequest().getParameter("pn350E");
        Object table1 = getRequest().getParameter("table1");
        //lay danh sach trong lan chay dau tien
        if (fn100N != null) {
            this.fn100 = Integer.parseInt(fn100N.toString());
            listN350N = getListAllN350ByID();
        }

        if (getRequest().getParameter("action") != null) {
            String action = getRequest().getParameter("action");
            if (action.equals("add")) {
                 Object objfn100A = getRequest().getParameter("fn100A");
                if(objfn100A!=null){
                    fn100 = Util.parseInt(objfn100A);
                }
                Object objPage = ContextUtil.getRequestParameter(("page"));
                  if (objPage != null) {
                    pagereload = objPage.toString();
                }
                  System.out.println(fn100+"-----------------------PAGE-------------"+pagereload);
                if (table1 != null) {
                    this.table1 = table1.toString();
                }
                this.flagedit = true;
                 Object stringtab1 = getRequest().getParameter("tab");
                if(stringtab1!=null){
                    stringtab = stringtab1.toString();
                }
            } else if (action.equals("saveN350")) {
               // this.fn100 = EmployeeController.idEmp;
            } else if (action.equals("edit")) {
                Object stringtab1 = getRequest().getParameter("tab");
                if(stringtab1!=null){
                    stringtab = stringtab1.toString();
                }
                 Object objPage = ContextUtil.getRequestParameter(("page"));
                  if (objPage != null) {
                    pagereload = objPage.toString();
                }
                  System.out.println(fn100+"-----------------------PAGE-------------"+pagereload);
                this.fn100 = Integer.parseInt(fn100E.toString());
                //this.pn350 = Integer.parseInt(pn350E.toString());
                if (table1 != null) {
                    this.table1 = table1.toString();
                } else {
                }
                //hien thi thong tin
                listN350N = getListAllN350ByID();
                if (listN350N.size() > 0) {
                    Iterator ic = listN350N.iterator();
                    while (ic.hasNext()) {
                        N350 n350 = (N350) ic.next();
                        this.pn350 = n350.getPN350();
                        this.nn355 = n350.getNN355();
                        //cmnd
                        if (nn355 == 1) {
                            nn355c = true;
                            this.nv354c = n350.getNv354();
                            this.nv351c = n350.getNv351();
                            this.nd352c = n350.getNd352();
                            this.nd353c = n350.getNd353();
                            this.pn350c = n350.getPN350();
                        }//passport
                        if (nn355 == 2) {
                            nn355p = true;
                            this.nv354p = n350.getNv354();
                            this.nv351p = n350.getNv351();
                            this.nd352p = n350.getNd352();
                            this.nd353p = n350.getNd353();
                            this.pn350p = n350.getPN350();
                        }


                        //this.nv351 = n350.getNv351();
                        //this.nd352 = n350.getNd352();
                        //this.nd353 = n350.getNd353();
                        //this.nv354 = n350.getNv354();

                    }

                } else {
                    //  this.fn100 = fn1000;
                }
                this.flagedit = false;
            } else if (action.equals("editajaxpage")) {
                //hien thi thong tin
                listN350N = getListAllN350ByID();
                if (listN350N.size() > 0) {
                    Iterator ic = listN350N.iterator();
                    while (ic.hasNext()) {
                        N350 n350 = (N350) ic.next();
                        this.pn350 = n350.getPN350();
                        this.nn355 = n350.getNN355();
                        //cmnd
                        if (nn355 == 1) {
                            nn355c = true;
                            this.nv354c = n350.getNv354();
                            this.nv351c = n350.getNv351();
                            this.nd352c = n350.getNd352();
                            this.nd353c = n350.getNd353();
                            this.pn350c = n350.getPN350();
                        }//passport
                        if (nn355 == 2) {
                            nn355p = true;
                            this.nv354p = n350.getNv354();
                            this.nv351p = n350.getNv351();
                            this.nd352p = n350.getNd352();
                            this.nd353p = n350.getNd353();
                            this.pn350p = n350.getPN350();
                        }


                        //this.nv351 = n350.getNv351();
                        //this.nd352 = n350.getNd352();
                        //this.nd353 = n350.getNd353();
                        //this.nv354 = n350.getNv354();

                    }

                }
            }
        }
        if (fn100M != null && pn350E == null) {
            this.fn100 = Integer.parseInt(fn100M.toString());
            // listN350N = getListAllN350ByID(0, fn100);
        }
        if (fn100M != null && pn350E != null) {
            this.fn100 = Integer.parseInt(fn100M.toString());
            this.pn350 = Integer.parseInt(pn350E.toString());
            this.table1 = table1.toString();
            // listN350N = getListAllN350ByID(pn350, fn100);
        }

        if (fn100N != null) {
            this.fn100 = Integer.parseInt(fn100N.toString());
            //  listN350N = getListAllN350ByID(0, fn100);
        } else {
            if (fn100 != 0 && pn350 != 0) {
                //listN350N = getListAllN350ByID(pn350, fn100);
            }
        }

    }

    public void saveN350() throws ParseException {
        System.out.println("----fn100-------------" + this.fn100);
        try {
            int count1 = 0;
            int count2 = 0;

            if (nn355c == true) {
                String dateStart = ContextUtil.getRequestParameter("dateStart");
                if (!dateStart.equals("")) {
                    nd352c = Util.parseDate(dateStart, "dd/MM/yyyy");
                } else {
                    nd352c = null;
                } 
               // System.out.println("----nv351c-------------" + this.nv351c.length());
                if(this.nv351c !=""){
                   count1 = n350Store.updateN350By(this.pn350c, fn100, this.getNv351c(), Util.toSQLDate(this.nd352c), Util.toSQLDate(this.nd353c), this.nv354c, 1, 7, 99, getLogin().getUserName()); 
                    setMessageform(getResourceString("success"));
                }else{
                   setMessageform(getResourceString("error")); 
                }
                

            }
            if (nn355p == true) {
                String dateStart1 = ContextUtil.getRequestParameter("dateStart1");
                if (!dateStart1.equals("")) {
                    nd352p = Util.parseDate(dateStart1, "dd/MM/yyyy");
                } else {
                    nd352p = null;
                }
                String dateEnd1 = ContextUtil.getRequestParameter("dateEnd1");
                if (!dateEnd1.equals("")) {
                    nd353p = Util.parseDate(dateEnd1, "dd/MM/yyyy");
                } else {
                    nd353p = null;
                }
                if(nv351p!=""){
                    count2 = n350Store.updateN350By(this.pn350p, fn100, this.nv351p, Util.toSQLDate(this.nd352p), Util.toSQLDate(this.nd353p), this.nv354p, 2, 7, 99, getLogin().getUserName());
                     setMessageform(getResourceString("success"));
                }else{
                   setMessageform(getResourceString("error")); 
                }
                

            }
            if (table1 != null) {
                System.out.println("---pagereload" + pagereload);
                if (table1.equals("myprofile")) {
                    System.out.println("---222222222");
                    if (table1.equals("myprofile")) {
                        
                        showGlobalReport(true, getResourceString("datasavesuccessful"));
                        sendRedirectUrlTab("myprofile.jsp?fn100M=" + fn100+"&linkmyprofile="+Util.convertMD5(getRequest().getRequestURI()));
                    } else {
                        showGlobalReport(true, getResourceString("datasavesuccessful"));
                        sendRedirectURL(pagereload, fn100, 3);
                    }
                } else if (pagereload.equals("customer.jsp")) {
                    showGlobalReport(true, getResourceString("datasavesuccessful"));
                  //  sendRedirectURL(pagereload, fn100, 3);
                     sendRedirectURLnew(pagereload, stringtab, fn100);
                } else if (pagereload.equals("employee.jsp")) {
                    showGlobalReport(true, getResourceString("datasavesuccessful"));
                    sendRedirectURL(pagereload, fn100, 3);
                }else if (pagereload.equals("employeenew.jsp")) {
                    showGlobalReport(true, getResourceString("datasavesuccessful"));
                    //sendRedirectURL(pagereload, fn100, 3);
                    sendRedirectURLnew(pagereload, stringtab, fn100);
                }else{
                     showGlobalReport(true, getResourceString("datasavesuccessful"));
                    
                    sendRedirectURLnew(pagereload, stringtab, fn100);
                }
            }

        } catch (Exception ex) {
            showGlobalReport(true, getResourceString("datasuccessfail"));
            System.out.println("-----errro" + ex.getMessage());
        }
    }

    public List<N350> getListAllN350ByID() {

        try {
            n350Store = new N350Store();
            List<N350> lst = n350Store.findN350All(7, 99, 0, fn100, 0, getLogin().getUserName());
            System.out.println("00------> size CMND" + lst.size() + "--" + pn350 + "--" + this.nn355);
            if (lst.size() > 0) {
                add = false;
                System.out.println("---render---" + isRenderAdd() + "----" + lst.size());
            }

            return lst;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }

    }

    public int deleteN350(int id) {
        if (id != 0) {
            int rows = 0;
            rows = n350Store.deleteN350(id, getLogin().getUserName());
            return rows;
        } else {
            return 0;
        }

    }

    /**
     * @return the pn350
     */
    public int getPn350() {
        return pn350;
    }

    /**
     * @param pn350 the pn350 to set
     */
    public void setPn350(int pn350) {
        this.pn350 = pn350;
    }

    /**
     * @return the fn100
     */
    public int getFn100() {
        if(fn100==0){
           if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                fn100 = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
        }
        return fn100;
    }

    /**
     * @param fn100 the fn100 to set
     */
    public void setFn100(int fn100) {
        this.fn100 = fn100;
    }

    /**
     * @return the nv351
     */
    public String getNv351() {
        return nv351;
    }

    /**
     * @param nv351 the nv351 to set
     */
    public void setNv351(String nv351) {
        this.nv351 = nv351;
    }

    /**
     * @return the nd352
     */
    public Date getNd352() {
        return nd352;
    }

    /**
     * @param nd352 the nd352 to set
     */
    public void setNd352(Date nd352) {
        this.nd352 = nd352;
    }

    /**
     * @return the nd353
     */
    public Date getNd353() {
        return nd353;
    }

    /**
     * @param nd353 the nd353 to set
     */
    public void setNd353(Date nd353) {
        this.nd353 = nd353;
    }

    /**
     * @return the nv354
     */
    public String getNv354() {
        return nv354;
    }

    /**
     * @param nv354 the nv354 to set
     */
    public void setNv354(String nv354) {
        this.nv354 = nv354;
    }

    /**
     * @return the fh000
     */
    public int getFh000() {
        return fh000;
    }

    /**
     * @param fh000 the fh000 to set
     */
    public void setFh000(int fh000) {
        this.fh000 = fh000;
    }

    /**
     * @return the fh200
     */
    public int getFh200() {
        return fh200;
    }

    /**
     * @param fh200 the fh200 to set
     */
    public void setFh200(int fh200) {
        this.fh200 = fh200;
    }

    /**
     * @return the n350Inf
     */
    public N350Inf getN350Inf() {
        return n350Inf;
    }

    /**
     * @param n350Inf the n350Inf to set
     */
    public void setN350Inf(N350Inf n350Inf) {
        this.n350Inf = n350Inf;
    }

    /**
     * @return the temp
     */
    public String getTemp() {
        return temp;
    }

    /**
     * @param temp the temp to set
     */
    public void setTemp(String temp) {
        this.temp = temp;
    }

    /**
     * @return the temp_active
     */
    public String getTemp_active() {
        return temp_active;
    }

    /**
     * @param temp_active the temp_active to set
     */
    public void setTemp_active(String temp_active) {
        this.temp_active = temp_active;
    }

    /**
     * @return the listN350N
     */
    public List<N350> getListN350N() {
        return listN350N;
    }

    /**
     * @param listN350N the listN350N to set
     */
    public void setListN350N(List<N350> listN350N) {
        this.listN350N = listN350N;
    }

    /**
     * @return the pagereload
     */
    public String getPagereload() {
        return pagereload;
    }

    /**
     * @param pagereload the pagereload to set
     */
    public void setPagereload(String pagereload) {
        this.pagereload = pagereload;
    }

    /**
     * @return the flagedit
     */
    public boolean isFlagedit() {
        return flagedit;
    }

    /**
     * @param flagedit the flagedit to set
     */
    public void setFlagedit(boolean flagedit) {
        this.flagedit = flagedit;
    }

    /**
     * @return the table1
     */
    public String getTable1() {
        return table1;
    }

    /**
     * @param table1 the table1 to set
     */
    public void setTable1(String table1) {
        this.table1 = table1;
    }

    /**
     * @return the nn355
     */
    public int getNn355() {
        return nn355;
    }

    /**
     * @param nn355 the nn355 to set
     */
    public void setNn355(int nn355) {
        this.nn355 = nn355;
    }

    /**
     * @return the demo
     */
    public String getDemo() {
        return demo;
    }

    /**
     * @param demo the demo to set
     */
    public void setDemo(String demo) {
        this.demo = demo;
    }

    /**
     * @return the add
     */
    public boolean isAdd() {
        return add;
    }

    /**
     * @param add the add to set
     */
    public void setAdd(boolean add) {
        this.add = add;
    }

    /**
     * @return the edit
     */
    public boolean isEdit() {
        return edit;
    }

    /**
     * @param edit the edit to set
     */
    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    /**
     * @return the delete
     */
    public boolean isDelete() {
        return delete;
    }

    /**
     * @param delete the delete to set
     */
    public void setDelete(boolean delete) {
        this.delete = delete;
    }

    /**
     * @return the nv354p
     */
    public String getNv354p() {
        return nv354p;
    }

    /**
     * @param nv354p the nv354p to set
     */
    public void setNv354p(String nv354p) {
        this.nv354p = nv354p;
    }

    /**
     * @return the nv354c
     */
    public String getNv354c() {
        return nv354c;
    }

    /**
     * @param nv354c the nv354c to set
     */
    public void setNv354c(String nv354c) {
        this.nv354c = nv354c;
    }

    /**
     * @return the nv351p
     */
    public String getNv351p() {
        return nv351p;
    }

    /**
     * @param nv351p the nv351p to set
     */
    public void setNv351p(String nv351p) {
        this.nv351p = nv351p;
    }

    /**
     * @return the nv351c
     */
    public String getNv351c() {
        return nv351c;
    }

    /**
     * @param nv351c the nv351c to set
     */
    public void setNv351c(String nv351c) {
        this.nv351c = nv351c;
    }

    /**
     * @return the nn355c
     */
    public boolean isNn355c() {
        return nn355c;
    }

    /**
     * @param nn355c the nn355c to set
     */
    public void setNn355c(boolean nn355c) {
        this.nn355c = nn355c;
    }

    /**
     * @return the nn355p
     */
    public boolean isNn355p() {
        return nn355p;
    }

    /**
     * @param nn355p the nn355p to set
     */
    public void setNn355p(boolean nn355p) {
        this.nn355p = nn355p;
    }

    /**
     * @return the nd352c
     */
    public Date getNd352c() {
        return nd352c;
    }

    /**
     * @param nd352c the nd352c to set
     */
    public void setNd352c(Date nd352c) {
        this.nd352c = nd352c;
    }

    /**
     * @return the nd353c
     */
    public Date getNd353c() {
        return nd353c;
    }

    /**
     * @param nd353c the nd353c to set
     */
    public void setNd353c(Date nd353c) {
        this.nd353c = nd353c;
    }

    /**
     * @return the nd352p
     */
    public Date getNd352p() {
        return nd352p;
    }

    /**
     * @param nd352p the nd352p to set
     */
    public void setNd352p(Date nd352p) {
        this.nd352p = nd352p;
    }

    /**
     * @return the nd353p
     */
    public Date getNd353p() {
        return nd353p;
    }

    /**
     * @param nd353p the nd353p to set
     */
    public void setNd353p(Date nd353p) {
        this.nd353p = nd353p;
    }

    /**
     * @return the pn350c
     */
    public int getPn350c() {
        return pn350c;
    }

    /**
     * @param pn350c the pn350c to set
     */
    public void setPn350c(int pn350c) {
        this.pn350c = pn350c;
    }

    /**
     * @return the pn350p
     */
    public int getPn350p() {
        return pn350p;
    }

    /**
     * @param pn350p the pn350p to set
     */
    public void setPn350p(int pn350p) {
        this.pn350p = pn350p;
    }

    /**
     * @return the renderAdd
     */
    public boolean isRenderAdd() {
        return renderAdd;
    }

    /**
     * @param renderAdd the renderAdd to set
     */
    public void setRenderAdd(boolean renderAdd) {
        this.renderAdd = renderAdd;
    }

    /**
     * @return the messageform
     */
    public String getMessageform() {
        return messageform;
    }

    /**
     * @param messageform the messageform to set
     */
    public void setMessageform(String messageform) {
        this.messageform = messageform;
    }

    /**
     * @return the listoftabN350
     */
    public List<N350> getListoftabN350() {
        try {
            if(SessionProvider.getSessionMapValue("PN100HD")!=null){
                fn100 = Util.parseInt(SessionProvider.getSessionMapValue("PN100HD"));
            }
             System.out.println("----------THAM SO-----N350------"+fn100+","+ 0+","+ getLogin().getUserName());
            listoftabN350 =  n350Store.findN350All(getLogin().getModule(),getLogin().getCompanyNo(), 0, fn100, 0, getLogin().getUserName());
              System.out.println("-----SIZE---------"+listoftabN350.size());
        } catch (Exception e) {
        }
        return listoftabN350;
    }

    /**
     * @param listoftabN350 the listoftabN350 to set
     */
    public void setListoftabN350(List<N350> listoftabN350) {
        this.listoftabN350 = listoftabN350;
    }

    /**
     * @return the stringtab
     */
    public String getStringtab() {
        
        return stringtab;
    }

    /**
     * @param stringtab the stringtab to set
     */
    public void setStringtab(String stringtab) {
        this.stringtab = stringtab;
    }
}
